#ifndef _OBJET_MNT_H
#define _OBJET_MNT_H

#include <fstream>
#include <iostream>
#include <string>
#include <math.h>
#include "readpng.h"
#include "Matiere.h"

#include "Objet3D.h"

using namespace std;

class ObjetMNT : public Objet3D 
{
 private :
  Matiere m_m;
 public :
  gl_texture_t *Tex;

  void charger()
  {
    ifstream file(_filename.c_str(), ios::in);
    int ncols, nrows, xllcorner, yllcorner, cellsize;
    float NODATA_value;
    string trash;
    _minZ = 1000000;
    if(_filename=="")cout<<"aucun fichier associé"<<endl;
    else{
      if(file){
        file >> trash;
        file >> ncols;
        file >> trash;
        file >> nrows;
        _nb_vertices=ncols*nrows;
        file >> trash;
        file >> xllcorner;
        file >> trash;
        file >> yllcorner;
        file >> trash;
        file >> cellsize;
        file >> trash;
        file >> NODATA_value;

        _vertices = new float[ncols*nrows*3];

        for(int i=0; i<nrows; i++){
          for (int j=0;j<ncols*3;j+=3){
            // ncols*i*3+j = coord du point mais dans le tableau 1 Dimension _vertices
            _vertices[(ncols*i*3)+j]=(i); 
            _vertices[(ncols*i*3)+j+1]=(j/3);
            file >> _vertices[(ncols*i*3)+j+2];
            
            if(_vertices[(ncols*i*3)+j+2] < _minZ && 
               _vertices[(ncols*i*3)+j+2] != NODATA_value){ 
              _minZ = _vertices[(ncols*i*3)+j+2];
            }
          }
        }

        for (int a=2; a<_nb_vertices*3; a+=3){
          if( _vertices[a] == NODATA_value ){
            _vertices[a]=_minZ;
          }
        }

        gen_index(); 
        gen_texcoord();
        chargerTexture();
      }
      else
        cout<<"erreur chargement fichier !!!"<<endl;
    }
    genNormal();
  }

  void chargerTexture(){
        glEnable(GL_TEXTURE_2D);
        if(_texname!=""){
        Tex = ReadPNGFromFile(_texname.c_str());
        glGenTextures(1, &Tex->id);

        glBindTexture(GL_TEXTURE_2D,Tex->id);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        glTexImage2D (GL_TEXTURE_2D, 
                      0, 
                      Tex->internalFormat, 
                      Tex->width, 
                      Tex->height, 
                      0, 
                      Tex->format, 
                      GL_UNSIGNED_BYTE, 
                      Tex->texels);
        }
        else 
          cout<<"Texture non chargée !"<<endl;
  }

  void dessiner(float scale = 1.)
  {   
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);  
    glCullFace(GL_BACK);
    glEnable(GL_TEXTURE_2D);
    m_m.ActiveMat();
    glPushMatrix();
    glColor3ub(255,255,255);
    glBindTexture(GL_TEXTURE_2D,Tex->id);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    
    glVertexPointer(3,GL_FLOAT,0,_vertices); 
    glTexCoordPointer(2, GL_FLOAT, 0, _texcoord);       
    glNormalPointer(GL_FLOAT,0,_normals);
    
    glTranslatef(_posX, _posY, _posZ);
    glRotatef(_angleX,1.,0.,0.); 
    glRotatef(_angleY,0.,1.,0.); 
    glRotatef(_angleZ,0.,0.,1.);

    glDrawElements(GL_TRIANGLES,_nb_index*3,GL_UNSIGNED_INT,_index);
 
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);

    glDisable(GL_TEXTURE_2D); 
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    m_m.DisactiveMat();

    //drawNormals();
    glPopMatrix();

  }

  void set_mat_Ambiente(GLfloat a, GLfloat b, GLfloat c, GLfloat d){
    m_m.set_Matiere_Ambiente(a, b, c, d);
  }
  void set_mat_Diffuse(GLfloat a, GLfloat b, GLfloat c, GLfloat d){    
    m_m.set_Matiere_Diffuse(a, b, c, d);
  }
  void set_mat_Speculaire(GLfloat a, GLfloat b, GLfloat c, GLfloat d){
    m_m.set_Matiere_Speculaire(a, b, c, d);
  }
  void set_shininess(int sh){
    m_m.set_Shininess(sh);
  }
  void get_All_mat(){
    m_m.get_All_Mat();
  }
  
  void gen_index()
  {
    int ncols=sqrt(_nb_vertices);
    int nrows=sqrt(_nb_vertices);

    _nb_index= 2*(ncols-1)*(nrows-1);
    _index = new unsigned int[_nb_index*3];


    int k=0;

    for(int i=0; i<(nrows-1); i++)
      {
        for (int j=1;j<(ncols);j++)
          {
            _index[k]  = (j-1)+i*ncols;
            k++;
            _index[k]= (j-1)+(i+1)*ncols;
            k++;
            _index[k]= (j)+i*ncols;
            k++;

            _index[k]= (j)+i*ncols;
            k++;
            _index[k]= (j-1)+(i+1)*ncols;
            k++;
            _index[k]= (j)+(i+1)*ncols;
            k++;
          }     
      } 
  }

  void gen_texcoord()
  {
    if(_nb_vertices!=0)
      {
        _nb_texcoord=_nb_vertices*2;
        _texcoord =new float[_nb_texcoord];
        int ncols=sqrt(_nb_vertices);

        int k=0;
        for (int i=0;i<ncols;i++)
          {
            for (int j=0; j<ncols; j++)
              {
                _texcoord[k]=(float)((float)j/(ncols));
                k++;
                _texcoord[k]=(float)(1.-(float)i/(ncols));
                k++;
              }
          }    
      }
    else
      {
        cout<<"aucun fichier chargé"<<endl;
      }
  }


};

#endif
